package com.health.fragment.focusgroup;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.health.R;
import com.health.db.FDbManager;
import com.health.fragment.NovaFragment;
import com.health.model.FengShiBingFollowupNew;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.ARCHIVE_GENERALCONDITION;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CD_DRUGCONDITION;
import com.health.tools.GlobalConts;
import com.health.view.spinner.NiceSpinner;
import com.health.viewscontrols.FWMYDPJActivity;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.PersonalFileActivity;
import com.health.viewscontrols.focusgroup.FengShiBingActivity;
import com.health.widget.EditTextWithDate;
import com.novabluetooth.services.BluetoothConstant;
import com.novabluetooth.views.ExamHistoryActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 风湿病
 * Created by John on 2019/7/31.
 */

public class FengShiBingFragment extends NovaFragment implements PersonalFileActivity.onFragmentSave, View.OnClickListener {
    private static final String TAG = "TNB_SfFragment";
    public static final int RequestXueya = 0;
    public static final int RequestTizhong = 1;
    public static final int RequestXuetang = 2;
    public static final int RequestXueYang = 3;
    private View cqsf2View = null;
    private FDbManager fdbmanger = null;

    // ------------------------------基本信息-----------------------------
    private EditTextWithDate ed_date;
    private NiceSpinner ed_sfys;// 随访医生
    private NiceSpinner nice_spinner_szfs;// 选择随诊方式
    //症状
    LinearLayout zhengzhuang_layout;
    CheckBox zhengzhuang_1;//无症状
    CheckBox zhengzhuang_2;//活动后呼吸困难
    CheckBox zhengzhuang_3;//乏力
    CheckBox zhengzhuang_4;//劳动耐力下降
    CheckBox zhengzhuang_5;//心悸
    CheckBox zhengzhuang_6;//哮喘
    CheckBox zhengzhuang_7;//食欲不振
    CheckBox zhengzhuang_8;//腹胀
    CheckBox zhengzhuang_9;//恶心
    CheckBox zhengzhuang_10;//慢性咳嗽
    CheckBox zhengzhuang_11;//咳痰
    CheckBox zhengzhuang_12;//口干
    CheckBox zhengzhuang_13;//眼干
    CheckBox zhengzhuang_14;//其他
    EditText zhengzhuang_qita;

    //体征
    private EditText ed_height;// 身高
    private EditText ed_tz;// 体重
    private EditText ed_tzzs;// 体质指数
    private Button btn_choose_tz;// 选择体重
   RadioGroup guanjie_fengshi_group;//受累关节及部位
    RadioButton guanjie_fengshi_no;
    RadioButton guanjie_fengshi_yes;
    EditText guanjie_fengshi_edt;
    EditText qita;//其他

   //生活方式指导
    //体育锻炼
    RadioGroup duanlain_pinlv_group;//锻炼频率
    String sport_duanlian_pinlvStr="0";
    RadioButton duanlianpinlv_meitian;
    RadioButton duanlianpinlv_meizhou;
    RadioButton duanlianpinlv_ouer;
    RadioButton duanlianpinlv_no;
    EditText meici_time;//每次锻炼时间
    EditText jianchi_time;//坚持锻炼时间
    EditText duanlian_fangshi;//锻炼方式
    //吸烟情况
    RadioGroup xiyan_group;//吸烟状况
    RadioButton xiyan_no;
    RadioButton xiyan_yijie;
    RadioButton xiyan_yes;
    String xiYanZhuangKuangStr="0";
    EditText xiyanliang;//吸烟量
    EditText start_time;//开始吸烟时间
    EditText end_time;//戒烟年龄


    private NiceSpinner nice_spinner_xltz;// 心理调整
    private NiceSpinner nice_spinner_zyxw;// 遵医行为
    EditText fuzhu_jiancha;//辅助检查
    private NiceSpinner nice_spinner_fyycx;// 服药依从性
    RadioGroup ywblfy_group;//服药不良反应
    RadioButton ywblfy_no;
    RadioButton ywblfy_yes;
    String ywblfyStr="DM01-11_1";
    EditText buliang_edt;
    private NiceSpinner nice_spinner_clsffl;// 此类随访分类
    private AutoCompleteTextView ed_ywmc1;// 药物名称1

    private EditText ed_ywmc1_day;// 每日 day
    private EditText ed_ywmc1_mg;// 每次 mg

    private AutoCompleteTextView ed_ywmc2;// 药物名称2

    private EditText ed_ywmc2_day;// 每日 day
    private EditText ed_ywmc2_mg;// 每次 mg
    private AutoCompleteTextView ed_ywmc3;// 药物名称3
    private NiceSpinner nice_spinner_dw1;// 药物单位1
    private NiceSpinner nice_spinner_dw2;// 药物单位2
    private NiceSpinner nice_spinner_dw3;// 药物单位3
    private EditText ed_ywmc3_day;// 每日 day
    private EditText ed_ywmc3_mg;// 每次 mg
    private RadioGroup zhuanzhen_group;//是否转诊
    private RadioButton zhuanzhen_no;
    private RadioButton zhuanzhen_yes;
    private LinearLayout zhuanzhen_layout;
    String isREFERRAL = "DM01-11_1";//是否转诊
    EditText jiankangjianyi;//健康评价
    private EditText ed_zzyy;// 转诊原因
    private EditText ed_jgkb;// 机构及科别
    private EditTextWithDate ed_xcsfsj;// 下次随访时间





    // ----------------------------------------------------------------------

    private List<BD_DD_INFO> szfs_list = new ArrayList<BD_DD_INFO>();
    private List<BD_DD_INFO> xltz_list = new ArrayList<BD_DD_INFO>();
    private List<BD_DD_INFO> zyxw_list = new ArrayList<BD_DD_INFO>();
    private List<BD_DD_INFO> ywycx_list = new ArrayList<BD_DD_INFO>();
    private List<BD_DD_INFO> ccsffl_list = new ArrayList<BD_DD_INFO>();
    private List<BD_DD_INFO> ywdw_list = new ArrayList<BD_DD_INFO>();
    public ARCHIVE_BASEINFO userInfo;
    private FengShiBingFollowupNew followUp;
    private FengShiBingFollowupNew readFollowup;
    // private static String strWhere = "IDCARD = '" +
    // NovaApplication.getUserInfo().getIDCARD() + "'";
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat dateFormatSFM = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private int from_followup = -1;
    // 定义字符串数组，作为提示的文本
    String[] tnb_yws = new String[]{"盐酸二甲双胍肠溶片", "盐酸二甲双胍片", "消渴丸", "盐酸格列本脲片", "格列齐特缓释片", "盐酸格列美脲片", "格列吡嗪片",
            "盐酸吡格列酮片", "盐酸二甲双胍格列本脲片", "盐酸二甲双胍格列本脲胶囊", "瑞格列奈片", "阿卡波糖片", "胰岛素", "格列齐特片", "格列喹酮片",};

    List<ARCHIVE_GENERALCONDITION> ags;



    @Override
    public View findViews(LayoutInflater inflater) {
        fdbmanger = NovaApplication.fdbmanger;
        userInfo = NovaApplication.getUserInfo();
        ags = fdbmanger.findAllByWhere(ARCHIVE_GENERALCONDITION.class, "IDCARD = '" + userInfo.getIDCARD() + "'");
        cqsf2View = inflater.inflate(R.layout.fengshibing_sfxx, null);
        initViews(cqsf2View);
        return cqsf2View;
    }

    public void initViews(View v) {
        ed_date = v.findViewById(R.id.ed_date);
        ed_sfys = v.findViewById(R.id.ed_sfys);
        nice_spinner_szfs = v.findViewById(R.id.nice_spinner_szfs);
        ed_date.setBefore(true);
        zhengzhuang_layout=v.findViewById(R.id.zhengzhuang_layout);
        zhengzhuang_1 = v.findViewById(R.id.zhengzhuang_1);
        zhengzhuang_2 = v.findViewById(R.id.zhengzhuang_2);
        zhengzhuang_3 = v.findViewById(R.id.zhengzhuang_3);
        zhengzhuang_4 = v.findViewById(R.id.zhengzhuang_4);
        zhengzhuang_5 = v.findViewById(R.id.zhengzhuang_5);
        zhengzhuang_6 = v.findViewById(R.id.zhengzhuang_6);
        zhengzhuang_7 = v.findViewById(R.id.zhengzhuang_7);
        zhengzhuang_8 = v.findViewById(R.id.zhengzhuang_8);
        zhengzhuang_9 = v.findViewById(R.id.zhengzhuang_9);
        zhengzhuang_10 = v.findViewById(R.id.zhengzhuang_10);
        zhengzhuang_11 = v.findViewById(R.id.zhengzhuang_11);
        zhengzhuang_12 = v.findViewById(R.id.zhengzhuang_12);
        zhengzhuang_13=v.findViewById(R.id.zhengzhuang_13);
        zhengzhuang_14=v.findViewById(R.id.zhengzhuang_14);
        zhengzhuang_qita = v.findViewById(R.id.zhengzhuang_qita);
        ed_height = v.findViewById(R.id.ed_height);
        ed_tz = v.findViewById(R.id.ed_tz);
        ed_tzzs = v.findViewById(R.id.ed_tzzs);
        btn_choose_tz = v.findViewById(R.id.btn_choose_tz);

       guanjie_fengshi_group=v.findViewById(R.id.guanjie_fengshi_group);
       guanjie_fengshi_no=v.findViewById(R.id.guanjie_fengshi_no);
       guanjie_fengshi_yes=v.findViewById(R.id.guanjie_fengshi_yes);

       guanjie_fengshi_edt=v.findViewById(R.id.guanjie_fengshi_edt);
        qita = v.findViewById(R.id.qita);
        duanlain_pinlv_group=v.findViewById(R.id.duanlain_pinlv_group);
        duanlianpinlv_meitian=v.findViewById(R.id.duanlianpinlv_meitian);
        duanlianpinlv_meizhou=v.findViewById(R.id.duanlianpinlv_meizhou);
        duanlianpinlv_ouer=v.findViewById(R.id.duanlianpinlv_ouer);
        duanlianpinlv_no=v.findViewById(R.id.duanlianpinlv_no);
        meici_time=v.findViewById(R.id.meici_time);
        jianchi_time=v.findViewById(R.id.jianchi_time);
        duanlian_fangshi=v.findViewById(R.id.duanlian_fangshi);
        xiyan_group=v.findViewById(R.id.xiyan_group);
        xiyan_no=v.findViewById(R.id.xiyan_no);
        xiyan_yijie=v.findViewById(R.id.xiyan_yijie);
        xiyan_yes=v.findViewById(R.id.xiyan_yes);
        xiyanliang=v.findViewById(R.id.xiyanliang);
        start_time=v.findViewById(R.id.start_time);
        end_time=v.findViewById(R.id.end_time);

        nice_spinner_xltz = (NiceSpinner) v.findViewById(R.id.nice_spinner_xltz);
        nice_spinner_zyxw = (NiceSpinner) v.findViewById(R.id.nice_spinner_zyxw);
        fuzhu_jiancha=v.findViewById(R.id.fuzhu_jiancha);
        nice_spinner_fyycx = (NiceSpinner) v.findViewById(R.id.nice_spinner_fyycx);
        nice_spinner_clsffl = (NiceSpinner) v.findViewById(R.id.nice_spinner_clsffl);
        ywblfy_group=v.findViewById(R.id.ywblfy_group);
        ywblfy_no=v.findViewById(R.id.buliang_no);
        ywblfy_yes=v.findViewById(R.id.buliang_yes);

        buliang_edt=v.findViewById(R.id.buliang_edt);
        ed_ywmc1 = (AutoCompleteTextView) v.findViewById(R.id.ed_ywmc1);

        ed_ywmc1_day = (EditText) v.findViewById(R.id.ed_ywmc1_day);
        ed_ywmc1_mg = (EditText) v.findViewById(R.id.ed_ywmc1_mg);

        ed_ywmc2 = (AutoCompleteTextView) v.findViewById(R.id.ed_ywmc2);
        ed_ywmc2_day = (EditText) v.findViewById(R.id.ed_ywmc2_day);
        ed_ywmc2_mg = (EditText) v.findViewById(R.id.ed_ywmc2_mg);
        ed_ywmc3 = (AutoCompleteTextView) v.findViewById(R.id.ed_ywmc3);
        ed_ywmc3_day = (EditText) v.findViewById(R.id.ed_ywmc3_day);
        ed_ywmc3_mg = (EditText) v.findViewById(R.id.ed_ywmc3_mg);
        nice_spinner_dw1 = (NiceSpinner) v.findViewById(R.id.nice_spinner_dw1);
        nice_spinner_dw2 = (NiceSpinner) v.findViewById(R.id.nice_spinner_dw2);
        nice_spinner_dw3 = (NiceSpinner) v.findViewById(R.id.nice_spinner_dw3);
        jiankangjianyi=v.findViewById(R.id.jiankangjianyi);
        zhuanzhen_group=v.findViewById(R.id.zhuanzhen_group);
        zhuanzhen_no=v.findViewById(R.id.zhuanzhen_no);
        zhuanzhen_yes=v.findViewById(R.id.zhuanzhen_yes);
        zhuanzhen_layout=v.findViewById(R.id.zhuanzhen_layout);
        ed_zzyy = (EditText) v.findViewById(R.id.ed_zzyy);
        ed_jgkb = (EditText) v.findViewById(R.id.ed_jgkb);
        ed_xcsfsj = (EditTextWithDate) v.findViewById(R.id.ed_xcsfsj);
        ed_xcsfsj.setAfter(true, 0);


    }

    public void setZhengZhuang(boolean isChecked) {
        zhengzhuang_2.setEnabled(isChecked);
        zhengzhuang_3.setEnabled(isChecked);
        zhengzhuang_4.setEnabled(isChecked);
        zhengzhuang_5.setEnabled(isChecked);
        zhengzhuang_6.setEnabled(isChecked);
        zhengzhuang_7.setEnabled(isChecked);
        zhengzhuang_8.setEnabled(isChecked);
        zhengzhuang_9.setEnabled(isChecked);
        zhengzhuang_10.setEnabled(isChecked);
        zhengzhuang_11.setEnabled(isChecked);
        zhengzhuang_12.setEnabled(isChecked);
        zhengzhuang_13.setEnabled(isChecked);
        zhengzhuang_14.setEnabled(isChecked);

        if (!isChecked) {
            zhengzhuang_qita.setText("");
            zhengzhuang_qita.setVisibility(View.INVISIBLE);
            zhengzhuang_2.setChecked(false);
            zhengzhuang_3.setChecked(false);
            zhengzhuang_4.setChecked(false);
            zhengzhuang_5.setChecked(false);
            zhengzhuang_6.setChecked(false);
            zhengzhuang_7.setChecked(false);
            zhengzhuang_8.setChecked(false);
            zhengzhuang_9.setChecked(false);
            zhengzhuang_10.setChecked(false);
            zhengzhuang_11.setChecked(false);
            zhengzhuang_12.setChecked(false);
            zhengzhuang_13.setChecked(false);
            zhengzhuang_14.setChecked(false);

        }



    }
    /**
     * 获取症状保存值
     */
    public String getZhengZhuang(){
        String zhengZhuangStr="";
        if(zhengzhuang_1.isChecked()){
            zhengZhuangStr="DM04-17_000.0";
        }else{
            if(zhengzhuang_2.isChecked()){
                zhengZhuangStr="DM04-17_1.0";
            }
            if(zhengzhuang_3.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_2.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_2.0";
                }

            }
            if(zhengzhuang_4.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_3.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_3.0";
                }
            }
            if(zhengzhuang_5.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_4.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_4.0";
                }
            }
            if(zhengzhuang_6.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_5.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_5.0";
                }
            }
            if(zhengzhuang_7.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_6.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_6.0";
                }
            }
            if(zhengzhuang_8.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_7.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_7.0";
                }
            }
            if(zhengzhuang_9.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_8.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_8.0";
                }
            }
            if(zhengzhuang_10.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_9.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_9.0";
                }
            }
            if(zhengzhuang_11.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_10.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_10.0";
                }
            }
            if(zhengzhuang_12.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_11.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_11.0";
                }
            }
            if(zhengzhuang_13.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_12.0";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_12.0";
                }
            }
            if(zhengzhuang_14.isChecked()){
                if(TextUtils.isEmpty(zhengZhuangStr)){
                    zhengZhuangStr="DM04-17_999.9";
                }else{
                    zhengZhuangStr=zhengZhuangStr+",DM04-17_999.9";
                }
            }
        }
        return zhengZhuangStr;
    }


    public void setZhengZhuangData(String zhengzhuang){
        if(!TextUtils.isEmpty(zhengzhuang)){
            if(zhengzhuang.length()==1){
                if("DM04-17_000.0".equals(zhengzhuang)){
                    zhengzhuang_1.setChecked(true);
                }else  if("DM04-17_1.0".equals(zhengzhuang)){
                    zhengzhuang_2.setChecked(true);
                }else  if("DM04-17_2.0".equals(zhengzhuang)){
                    zhengzhuang_3.setChecked(true);
                }else  if("DM04-17_3.0".equals(zhengzhuang)){
                    zhengzhuang_4.setChecked(true);
                }else  if("DM04-17_4.0".equals(zhengzhuang)){
                    zhengzhuang_5.setChecked(true);
                }else  if("DM04-17_5.0".equals(zhengzhuang)){
                    zhengzhuang_6.setChecked(true);
                }else  if("DM04-17_6.0".equals(zhengzhuang)){
                    zhengzhuang_7.setChecked(true);
                }else  if("DM04-17_7.0".equals(zhengzhuang)){
                    zhengzhuang_8.setChecked(true);
                }else  if("DM04-17_8.0".equals(zhengzhuang)){
                    zhengzhuang_9.setChecked(true);
                }else  if("DM04-17_9.0".equals(zhengzhuang)){
                    zhengzhuang_10.setChecked(true);
                }else  if("DM04-17_10.0".equals(zhengzhuang)){
                    zhengzhuang_11.setChecked(true);
                }else  if("DM04-17_11.0".equals(zhengzhuang)){
                    zhengzhuang_12.setChecked(true);
                }else  if("DM04-17_12.0".equals(zhengzhuang)){
                    zhengzhuang_13.setChecked(true);
                }else  if("DM04-17_999.9".equals(zhengzhuang)){
                    zhengzhuang_14.setChecked(true);
                    zhengzhuang_qita.setVisibility(View.VISIBLE);
                }
            }else{
                String ss[]=zhengzhuang.split(",");
                for(String s:ss){
                    if("DM04-17_000.0".equals(s)){
                        zhengzhuang_1.setChecked(true);
                    }else  if("DM04-17_1.0".equals(s)){
                        zhengzhuang_2.setChecked(true);
                    }else  if("DM04-17_2.0".equals(s)){
                        zhengzhuang_3.setChecked(true);
                    }else  if("DM04-17_3.0".equals(s)){
                        zhengzhuang_4.setChecked(true);
                    }else  if("DM04-17_4.0".equals(s)){
                        zhengzhuang_5.setChecked(true);
                    }else  if("DM04-17_5.0".equals(s)){
                        zhengzhuang_6.setChecked(true);
                    }else  if("DM04-17_6.0".equals(s)){
                        zhengzhuang_7.setChecked(true);
                    }else  if("DM04-17_7.0".equals(s)){
                        zhengzhuang_8.setChecked(true);
                    }else  if("DM04-17_8.0".equals(s)){
                        zhengzhuang_9.setChecked(true);
                    }else  if("DM04-17_9.0".equals(s)){
                        zhengzhuang_10.setChecked(true);
                    }else  if("DM04-17_10.0".equals(s)){
                        zhengzhuang_11.setChecked(true);
                    }else  if("DM04-17_11.0".equals(s)){
                        zhengzhuang_12.setChecked(true);
                    }else  if("DM04-17_12.0".equals(s)){
                        zhengzhuang_13.setChecked(true);
                    }else  if("DM04-17_999.9".equals(s)){
                        zhengzhuang_14.setChecked(true);
                        zhengzhuang_qita.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    }
    private int getListIndex(String str, List<BD_DD_INFO> bds) {
        if (str != null && !str.equals("")) {

            for (int i = 0; i < bds.size(); i++) {
                if (str.equals(bds.get(i).getID())) {
                    return i;
                }
            }
        }
        return 0;
    }
    private String notNull(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        return str;
    }
    private void setDBData() {
        try {
            List<FengShiBingFollowupNew> list = fdbmanger.findAllByWhere(FengShiBingFollowupNew.class,
                    "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'");
            Log.e("tag", list.size() + "");
            Log.e("tag", "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'");
            if (list.size() > 0) {
                from_followup = 1;

                readFollowup = list.get(list.size() - 1);
                ed_date.setText(readFollowup.getFollowupDate());
                for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
                    if (readFollowup.getFollowupdoctor().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
                        ed_sfys.setSelectedIndex(i);
                    }

                }
                String symptoms = readFollowup.getSymptom();
                setZhengZhuangData(symptoms);
                zhengzhuang_qita.setText(notNull(readFollowup.getSymptomother()));

                if (readFollowup.getFollowupway().contains("DM04-04")){
                    nice_spinner_szfs.setSelectedIndex(getListIndex(readFollowup.getFollowupway().replace("DM04-04","DM04-15"), szfs_list));
                }else{
                    nice_spinner_szfs.setSelectedIndex(getListIndex(readFollowup.getFollowupway(), szfs_list));
                }

                ed_tz.setText(readFollowup.getWeight());
                ed_tzzs.setText(readFollowup.getBmi());
                ed_height.setText(notNull(readFollowup.getHeight()));
                if("DM01-11_1".equals(readFollowup.getFengshiguanjie())){
                    guanjie_fengshi_no.setChecked(true);
                }else if("DM01-11_2".equals(readFollowup.getFengshiguanjie())){
                    guanjie_fengshi_yes.setChecked(true);
                    guanjie_fengshi_edt.setVisibility(View.VISIBLE);
                }
                guanjie_fengshi_edt.setText(notNull(readFollowup.getFengshiguanjiestr()));



                qita.setText(readFollowup.getPhysicalsymptomother());

                //生活方式指导
                if("DM100-05_1".equals(readFollowup.getTiyuDuanlianpinlv())){
                    duanlianpinlv_meitian.setChecked(true);

                }else if("DM100-05_2".equals(readFollowup.getTiyuDuanlianpinlv())){
                    duanlianpinlv_meizhou.setChecked(true);
                }else if("DM100-05_3".equals(readFollowup.getTiyuDuanlianpinlv())){
                    duanlianpinlv_ouer.setChecked(true);
                }else if("DM100-05_4".equals(readFollowup.getTiyuDuanlianpinlv())){
                    duanlianpinlv_no.setChecked(true);
                }
                meici_time.setText(notNull(readFollowup.getMeiciDuanlianTime()));
                jianchi_time.setText(notNull(readFollowup.getJianchiDuanlianTime()));
                duanlian_fangshi.setText(notNull(readFollowup.getDuanlianfangshi()));
                if("DM100-07_1".equals(readFollowup.getXiyanzhuangkuang())){
                    xiyan_no.setChecked(true);
                }else if("DM100-07_2".equals(readFollowup.getXiyanzhuangkuang())){
                    xiyan_yijie.setChecked(true);
                }else if("DM100-07_3".equals(readFollowup.getXiyanzhuangkuang())){
                    xiyan_yes.setChecked(true);
                }
                xiyanliang.setText(notNull(readFollowup.getDailysmokenum()));
                start_time.setText(notNull(readFollowup.getStartXiyan()));
                end_time.setText(notNull(readFollowup.getEndXiyan()));


                nice_spinner_xltz.setSelectedIndex(getListIndex(readFollowup.getPsychoadjustment(), xltz_list));
                nice_spinner_zyxw.setSelectedIndex(getListIndex(readFollowup.getObeydoctorbehavior(), zyxw_list));
                fuzhu_jiancha.setText(notNull(readFollowup.getFuzhujiancha()));


                nice_spinner_clsffl.setSelectedIndex(getListIndex(readFollowup.getFollowuptype(), ccsffl_list));
                nice_spinner_fyycx.setSelectedIndex(getListIndex(readFollowup.getMedicationcompliance(), ywycx_list));
                if("DM01-11_1".equals(readFollowup.getAdr())){
                    ywblfy_no.setChecked(true);
                }else if("DM01-11_2".equals(readFollowup.getAdr())){
                    ywblfy_yes.setChecked(true);
                    buliang_edt.setVisibility(View.VISIBLE);
                }
                buliang_edt.setText(notNull(readFollowup.getAdrEx()));



                if(!TextUtils.isEmpty(readFollowup.getIsreferral())){
                    if("DM01-11_1".equals(readFollowup.getIsreferral())){
                        zhuanzhen_no.setChecked(true);
                        zhuanzhen_yes.setChecked(false);
                        zhuanzhen_layout.setVisibility(View.GONE);
                    }else if("DM01-11_2".equals(readFollowup.getIsreferral())){
                        zhuanzhen_yes.setChecked(true);
                        zhuanzhen_no.setChecked(false);
                        zhuanzhen_layout.setVisibility(View.VISIBLE);
                    }
                }
                jiankangjianyi.setText(notNull(readFollowup.getHealthAssess()));



                List<CD_DRUGCONDITION> drugList = fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
                        "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "'" + "AND TYPE = '8'");
                int size = drugList.size();
                EditText[] medicals = { ed_ywmc1, ed_ywmc2, ed_ywmc3 };
                EditText[] times = { ed_ywmc1_day, ed_ywmc2_day, ed_ywmc3_day };
                EditText[] doses = { ed_ywmc1_mg, ed_ywmc2_mg, ed_ywmc3_mg };
                if (drugList.size() > 0) {
                    for (int i = 0; i < size; i++) {
                        CD_DRUGCONDITION drug = drugList.get(i);
                        if (null == drug){
                            continue;
                        }
                        medicals[i].setText(notNull(drug.getNAME()));
                        times[i].setText(notNull(drug.getDAILYTIME()));
                        doses[i].setText(notNull(String.valueOf(drug.getEVERYTIMEMG())));
                        if (i == 0) {
                            nice_spinner_dw1.setSelectedIndex(getListIndex(drug.getRES1(), ywdw_list));
                        } else if (i == 1) {
                            nice_spinner_dw2.setSelectedIndex(getListIndex(drug.getRES1(), ywdw_list));
                        } else if (i == 2) {
                            nice_spinner_dw3.setSelectedIndex(getListIndex(drug.getRES1(), ywdw_list));
                        }
                    }
                }
                Log.e("***************","**************");
                ed_zzyy.setText(readFollowup.getReferralreason());
                ed_jgkb.setText(readFollowup.getReferralorg());
                ed_xcsfsj.setText(readFollowup.getNextfollowupDate());



            } else {
                from_followup = 0;
                ed_date.setText(dateFormat.format(new Date()));
                for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
                    if (userInfo.getCREATED_BY().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
                        ed_sfys.setSelectedIndex(i);
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void save() {
        // 随诊方式
        String str_id = szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID();
        Log.w(TAG, "随诊方式：" + str_id);
        if (TextUtils.isEmpty(str_id)) {
            nice_spinner_szfs.setBackgroundColor(Color.YELLOW);
            ((FengShiBingActivity)getActivity()).showTextDialog("请选择随诊方式");
            return;
        } else {
            nice_spinner_szfs.setBackgroundColor(Color.WHITE);
        }
        if(!TextUtils.isEmpty(end_time.getText())&&!TextUtils.isEmpty(start_time.getText())){
            int startAge=Integer.parseInt(start_time.getText().toString());
            int endAge=Integer.parseInt(end_time.getText().toString());
            if(startAge>endAge){
                ((FengShiBingActivity)getActivity()).showTextDialog("开始吸烟年龄，不能大于戒烟年龄");
                end_time.setText("");
                return;
            }

        }

        // 症状
        String str_zz = getZhengZhuang();
        if (TextUtils.isEmpty(str_zz)) {
            zhengzhuang_layout.setBackgroundColor(Color.YELLOW);
            ((FengShiBingActivity) getActivity()).showTextDialog("请选择症状");
            return;
        } else {
            zhengzhuang_layout.setBackgroundColor(Color.WHITE);
        }
        if(TextUtils.isEmpty(ed_height.getText())){
            ((FengShiBingActivity) getActivity()).showTextDialog("请输入身高");
            return;
        }
        if(TextUtils.isEmpty(ed_tz.getText())){
            ((FengShiBingActivity) getActivity()).showTextDialog("请输入体重");
            return;
        }

        // 下次随访时间
        String str_xcsfsj = ed_xcsfsj.getText().toString();
        if (TextUtils.isEmpty(str_xcsfsj)) {
            ed_xcsfsj.setBackgroundColor(Color.YELLOW);
            ((FengShiBingActivity) getActivity()).showTextDialog("请选择下次随访时间");
            return;
        } else {
            ed_xcsfsj.setBackgroundColor(Color.WHITE);
        }
        followUp=new FengShiBingFollowupNew();
        saveBase();
        saveTizheng();
        saveLife();
        saveMedicine();
        if(!TextUtils.isEmpty(zhengzhuang_qita.getText())){
            followUp.setSymptomother(zhengzhuang_qita.getText().toString());
        }else{
            followUp.setSymptomother("");
        }

        followUp.setIdcard(userInfo.getIDCARD());
        followUp.setLastUpdateBy(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());
        followUp.setLastUpdateDate(dateFormatSFM.format(new Date()));
        followUp.setIsUpdate("Y");

        	/*----------------------新添加----------------*/
        followUp.setIsreferral(isREFERRAL);
        if(!TextUtils.isEmpty(jiankangjianyi.getText())){
            followUp.setHealthAssess(jiankangjianyi.getText().toString());
        }else{
            followUp.setHealthAssess("");
        }


        if (from_followup == 1) {

            followUp.setUpdateorg(userInfo.getCREATEORG());
            followUp.setModified(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());

            fdbmanger.updata(followUp, "IDCARD='" + userInfo.getIDCARD() + "'");
        } else {
            followUp.setCreatedDate(dateFormatSFM.format(new Date()));
            followUp.setCreateorg(userInfo.getCREATEORG());
            followUp.setCreater(userInfo.getCREATER());
            followUp.setCreatedBy(userInfo.getCREATED_BY());
            fdbmanger.save(followUp);
            from_followup = 1;
        }
        if (GlobalConts.TYPE_JTYS.equals(NovaApplication.getType())) {
            new AlertDialog.Builder(getActivity()).setTitle("保存成功").setMessage("进行满意度评价")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(getContext(), FWMYDPJActivity.class);
                            intent.putExtra("type", "07");
                            intent.putExtra("szfs", szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID());
                            startActivity(intent);
                        }
                    }).show();
        } else if (GlobalConts.TYPE_GJJBGGWS.equals(NovaApplication.getType())) {
            ((FengShiBingActivity) getActivity()).showTextDialog("保存成功");
        }

    }
    private void saveMedicine() {

        List<CD_DRUGCONDITION> drugList = fdbmanger.findAllByWhere(CD_DRUGCONDITION.class,
                "IDCARD = '" + NovaApplication.getUserInfo().getIDCARD() + "' and TYPE='8'");
        for (CD_DRUGCONDITION drug : drugList) {
            fdbmanger.delete(drug);
        }
        String medicine1 = ed_ywmc1.getText().toString();
        String count1 = ed_ywmc1_day.getText().toString();
        String dose1 = ed_ywmc1_mg.getText().toString();
        String medicine2 = ed_ywmc2.getText().toString();
        String count2 = ed_ywmc2_day.getText().toString();
        String dose2 = ed_ywmc2_mg.getText().toString();
        String medicine3 = ed_ywmc3.getText().toString();
        String count3 = ed_ywmc3_day.getText().toString();
        String dose3 = ed_ywmc3_mg.getText().toString();



        CD_DRUGCONDITION drugCondition1 = new CD_DRUGCONDITION();
        CD_DRUGCONDITION drugCondition2 = new CD_DRUGCONDITION();
        CD_DRUGCONDITION drugCondition3 = new CD_DRUGCONDITION();
        drugCondition1.setIDCARD(NovaApplication.getUserInfo().getIDCARD());
        drugCondition2.setIDCARD(NovaApplication.getUserInfo().getIDCARD());
        drugCondition3.setIDCARD(NovaApplication.getUserInfo().getIDCARD());
        /** 类型 1高血压 2糖尿病 3精神疾病 4冠心病 5脑卒中 6.肺心病 7类风湿8.风湿病*/
        drugCondition1.setTYPE("8");
        drugCondition2.setTYPE("8");
        drugCondition3.setTYPE("8");
        if (!TextUtils.isEmpty(medicine1)) {
            drugCondition1.setRES0("0");
            drugCondition1.setNAME(medicine1);
            drugCondition1.setDAILYTIME(count1);
            drugCondition1.setEVERYTIMEMG(dose1);
            drugCondition1.setRES1(ywdw_list.get(nice_spinner_dw1.getSelectedIndex()).getID());
            fdbmanger.save(drugCondition1);
        }
        if (!TextUtils.isEmpty(medicine2)) {
            drugCondition2.setRES0("0");
            drugCondition2.setNAME(medicine2);
            drugCondition2.setDAILYTIME(count2);
            drugCondition2.setEVERYTIMEMG(dose2);
            drugCondition2.setRES1(ywdw_list.get(nice_spinner_dw2.getSelectedIndex()).getID());
            fdbmanger.save(drugCondition2);
        }
        if (!TextUtils.isEmpty(medicine3)) {
            drugCondition3.setRES0("0");
            drugCondition3.setNAME(medicine3);
            drugCondition3.setDAILYTIME(count3);
            drugCondition3.setEVERYTIMEMG(dose3);
            drugCondition3.setRES1(ywdw_list.get(nice_spinner_dw3.getSelectedIndex()).getID());
            fdbmanger.save(drugCondition3);
        }

        followUp.setReferralreason(ed_zzyy.getText().toString());
        followUp.setReferralorg(ed_jgkb.getText().toString());

        try {
            String edDate = ed_xcsfsj.getText().toString();
            Log.e("下次随访时间",edDate+"***********");
            followUp.setNextfollowupDate(edDate);
        }catch (Exception e){

        }
//		followUp.setNEXTFOLLOWUP_DATE(ed_xcsfsj.getDate());
    }
    private void saveLife() {




        if(!"0".equals(sport_duanlian_pinlvStr)){
            followUp.setTiyuDuanlianpinlv(sport_duanlian_pinlvStr);
        }
        if(!TextUtils.isEmpty(meici_time.getText())){
            followUp.setMeiciDuanlianTime(meici_time.getText().toString());
        }else{
            followUp.setMeiciDuanlianTime("");
        }
        if(!TextUtils.isEmpty(jianchi_time.getText())){
            followUp.setJianchiDuanlianTime(jianchi_time.getText().toString());
        }else{
            followUp.setJianchiDuanlianTime("");
        }

        if(!TextUtils.isEmpty(duanlian_fangshi.getText())){
            followUp.setDuanlianfangshi(duanlian_fangshi.getText().toString());
        }else{
            followUp.setDuanlianfangshi("");
        }
        if(!"0".equals(xiYanZhuangKuangStr)){
            followUp.setXiyanzhuangkuang(xiYanZhuangKuangStr);
        }
        if(!TextUtils.isEmpty(xiyanliang.getText())){
            followUp.setDailysmokenum(xiyanliang.getText().toString());
        }else{
            followUp.setDailysmokenum("");
        }


        if(!TextUtils.isEmpty(start_time.getText())){
            followUp.setStartXiyan(start_time.getText().toString());
        }else{
            followUp.setStartXiyan("");
        }
        if(!TextUtils.isEmpty(end_time.getText())){
            followUp.setEndXiyan(end_time.getText().toString());
        }else{
            followUp.setEndXiyan("");
        }


        followUp.setPsychoadjustment(xltz_list.get(nice_spinner_xltz.getSelectedIndex()).getID());
        followUp.setObeydoctorbehavior(zyxw_list.get(nice_spinner_zyxw.getSelectedIndex()).getID());
        if(!TextUtils.isEmpty(fuzhu_jiancha.getText())){
            followUp.setFuzhujiancha(fuzhu_jiancha.getText().toString());
        }else{
            followUp.setFuzhujiancha("");
        }










        followUp.setMedicationcompliance(ywycx_list.get(nice_spinner_fyycx.getSelectedIndex()).getID());
        followUp.setFollowuptype(ccsffl_list.get(nice_spinner_clsffl.getSelectedIndex()).getID());

        followUp.setAdr(ywblfyStr);
        if(!TextUtils.isEmpty(buliang_edt.getText())){
            followUp.setAdrEx(buliang_edt.getText().toString());
        }else{
            followUp.setAdrEx("");
        }





    }
    private void saveTizheng() {
        try {
            String tizhong = ed_tz.getText().toString();
            String tizhi = ed_tzzs.getText().toString();

            followUp.setHeight(ed_height.getText().toString());
            followUp.setWeight(tizhong);
            followUp.setBmi(tizhi);
            if(guanjie_fengshi_no.isChecked()){
                followUp.setFengshiguanjie("DM01-11_1");
            }else if(guanjie_fengshi_yes.isChecked()){
                followUp.setFengshiguanjie("DM01-11_2");
            }

            if(!TextUtils.isEmpty(guanjie_fengshi_edt.getText())){
                followUp.setFengshiguanjiestr(guanjie_fengshi_edt.getText().toString());
            }else{
                followUp.setFengshiguanjiestr("");
            }



            if(!TextUtils.isEmpty(qita.getText())){
                followUp.setPhysicalsymptomother(qita.getText().toString());
            }else{
                followUp.setPhysicalsymptomother("");
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void saveBase() {
        try {
            String edDate = ed_date.getText().toString();
            Log.e("随访时间",edDate+"***********");
            followUp.setFollowupDate(edDate);
        }catch (Exception e){

        }


        followUp.setSymptom(getZhengZhuang());

        followUp.setArchiveid("");
        followUp.setFollowupdoctor(NovaApplication.getDoctorList().get(ed_sfys.getSelectedIndex()).getUSER_ID());
        if (szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID().contains("DM04-04")){
            followUp.setFollowupway(szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID().replace("DM04-04","DM04-15"));
        }else{
            followUp.setFollowupway(szfs_list.get(nice_spinner_szfs.getSelectedIndex()).getID());
        }

    }
    @Override
    public void setupView(View view) {

    }

    @Override
    public void addListener() {
        zhengzhuang_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                setZhengZhuang(!isChecked);

            }
        });
        zhengzhuang_14.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    zhengzhuang_qita.setText("");
                    zhengzhuang_qita.setVisibility(View.INVISIBLE);
                } else {
                    zhengzhuang_qita.setVisibility(View.VISIBLE);
                }

            }
        });

        // 限制只能输如小数点后一位
        ed_tz.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {
                try {
                    String temp = edt.toString();
                    int posDot = temp.indexOf(".");
                    if (posDot <= 0)
                        return;
                    if (temp.length() - posDot - 1 > 1) {
                        edt.delete(posDot + 2, posDot + 3);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                if (ags.size() != 0) {
                    if (arg0.toString().isEmpty() || TextUtils.isEmpty(ags.get(0).getHEIGHT())) {
                        return;
                    }

                    double dh = Double.parseDouble(ags.get(0).getHEIGHT().toString()) / 100;
                    Log.e("++++++++", ags.get(0).getHEIGHT().toString());
                    double dw = Double.parseDouble(ed_tz.getText().toString());

                    java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");

                    ed_tzzs.setText(df.format(GlobalConts.getBMI(dh, dw)) + "");
                }
            }
        });


        guanjie_fengshi_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.guanjie_fengshi_no){
                    guanjie_fengshi_edt.setText("");
                    guanjie_fengshi_edt.setVisibility(View.INVISIBLE);
                }else{
                    guanjie_fengshi_edt.setVisibility(View.VISIBLE);
                }
            }
        });

        duanlain_pinlv_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.duanlianpinlv_no){
                    sport_duanlian_pinlvStr="DM100-05_4";
                    meici_time.setText("");
                    meici_time.setEnabled(false);
                    jianchi_time.setText("");
                    jianchi_time.setEnabled(false);
                    duanlian_fangshi.setText("");
                    duanlian_fangshi.setEnabled(false);
                }else{
                    meici_time.setEnabled(true);
                    jianchi_time.setEnabled(true);
                    duanlian_fangshi.setEnabled(true);
                    if(checkedId==R.id.duanlianpinlv_meitian){
                        sport_duanlian_pinlvStr="DM100-05_1";
                    }else if(checkedId==R.id.duanlianpinlv_meizhou){
                        sport_duanlian_pinlvStr="DM100-05_2";
                    }else if(checkedId==R.id.duanlianpinlv_ouer){
                        sport_duanlian_pinlvStr="DM100-05_3";
                    }
                }
            }
        });
        xiyan_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.xiyan_no){
                    xiYanZhuangKuangStr="DM100-07_1";
                    xiyanliang.setText("");
                    xiyanliang.setEnabled(false);
                    start_time.setText("");
                    start_time.setEnabled(false);
                    end_time.setText("");
                    end_time.setEnabled(false);
                }else if(checkedId==R.id.xiyan_yijie){
                    xiYanZhuangKuangStr="DM100-07_2";
                    xiyanliang.setText("");
                    xiyanliang.setEnabled(false);
                    start_time.setEnabled(true);
                    end_time.setEnabled(true);
                }else if(checkedId==R.id.xiyan_yes){
                    xiYanZhuangKuangStr="DM100-07_3";
                    xiyanliang.setEnabled(true);
                    start_time.setEnabled(true);
                    end_time.setText("");
                    end_time.setEnabled(false);
                }
            }
        });


        ywblfy_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.buliang_no){
                    ywblfyStr="DM01-11_1";
                    buliang_edt.setText("");
                    buliang_edt.setVisibility(View.INVISIBLE);
                }else{
                    ywblfyStr="DM01-11_2";
                    buliang_edt.setVisibility(View.VISIBLE);
                }
            }
        });

        zhuanzhen_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==zhuanzhen_no.getId()){
                    zhuanzhen_layout.setVisibility(View.GONE);
                    isREFERRAL="DM01-11_1";
                    ed_zzyy.setText("");
                    ed_jgkb.setText("");
                }else if(checkedId==zhuanzhen_yes.getId()){
                    isREFERRAL="DM01-11_2";
                    zhuanzhen_layout.setVisibility(View.VISIBLE);
                }
            }
        });



        changeBMI(ed_height, ed_tz, ed_tzzs);
        btn_choose_tz.setOnClickListener(this);

    }

    /**
     * 身高或者体重改变时，BMI的值随之发生改变
     **/
    private void changeBMI(final EditText etHeight, final EditText etWeight, final EditText etBMI) {
        etHeight.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(etWeight.getText().toString())) {
                    setBMI(etHeight, etWeight, etBMI);
                } else {
                    etBMI.setText("");
                }

                if (TextUtils.isEmpty(s.toString())) {
                    etBMI.setText("");
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etWeight.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(etHeight.getText().toString())) {
                    setBMI(etHeight, etWeight, etBMI);
                } else {
                    etBMI.setText("");
                }
                if (TextUtils.isEmpty(s.toString())) {
                    etBMI.setText("");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 设置BMI
     *
     * @author 王婷婷
     * @date 2017年4月5日 上午10:48:51
     */
    private void setBMI(EditText etHeight, EditText etWeight, EditText etBMI) {
        try {
            if (TextUtils.isEmpty(etHeight.getText().toString()) || TextUtils.isEmpty(etHeight.getText().toString())) {
                etBMI.setText("");
                return;
            }

            double dh = Double.parseDouble(etHeight.getText().toString()) / 100;
            double dw = Double.parseDouble(etWeight.getText().toString());

            java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");

            etBMI.setText(df.format(GlobalConts.getBMI(dh, dw)) + "");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void setData() {
        GlobalConts.doctorList(ed_sfys);
        bindSpinner("随诊方式", "PARENTID='DM04-15'");
        bindSpinner("心理情况", "PARENTID='DM01-69'");
        bindSpinner("药物依从性", "PARENTID='DM01-70'");
        bindSpinner("此次随访分类", "PARENTID='DM01-71'");
        bindSpinner("药物单位", "PARENTID='DM04-05'");
        setDBData();

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), ExamHistoryActivity.class);
        int id = v.getId();
       if (id == R.id.btn_choose_tz) {
            intent.putExtra("flag", BluetoothConstant.TIZHONG_TYPENAME);
            getActivity().startActivityForResult(intent, RequestTizhong);
        } else if (id == R.id.btn_choose_kfxtz) {
            intent.putExtra("flag", BluetoothConstant.XUETANG_TYPENAME);
            getActivity().startActivityForResult(intent, RequestXuetang);
        } else if (id == R.id.btn_xueyang) {
            intent.putExtra("flag", BluetoothConstant.XUEYANG_TYPENAME);
            getActivity().startActivityForResult(intent, RequestXueYang);
        } else {
        }
    }

    /**
     * 设置下拉
     *
     * @param flagName
     * @param strSql
     * @author 王婷婷
     * @date 2015年12月24日 上午9:19:28
     */

    private void bindSpinner(String flagName, String strSql) {
        List<BD_DD_INFO> sList = new ArrayList<BD_DD_INFO>();
        BD_DD_INFO bd = new BD_DD_INFO();
        bd.setDATAINFO("请选择");
        bd.setID("");
        sList.add(bd);

        List<BD_DD_INFO> sList1 = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
                strSql + " order by cast(SORT as int) asc");
        sList.addAll(sList1);

        if (flagName.equals("心理情况")) {

            int size = sList.size();
            if (size > 0) {
                xltz_list = new ArrayList<BD_DD_INFO>();
                zyxw_list = new ArrayList<BD_DD_INFO>();
                for (int i = 0; i < size; i++) {
                    String str_id = sList.get(i).getID();
                    if (str_id.equals("DM01-69_1") || str_id.equals("DM01-69_2") || str_id.equals("DM01-69_3")
                            || str_id.equals("")) {
                        xltz_list.add(sList.get(i));
                        zyxw_list.add(sList.get(i));
                    }
                }
            }

            nice_spinner_xltz.attachDataSource(xltz_list);
            nice_spinner_zyxw.attachDataSource(zyxw_list);
        } else if (flagName.equals("药物依从性")) {
            ywycx_list = sList;
            nice_spinner_fyycx.attachDataSource(ywycx_list);
        } else if (flagName.equals("此次随访分类")) {
            int size = sList.size();
            if (size > 0) {
                ccsffl_list = new ArrayList<BD_DD_INFO>();
                for (int i = 0; i < size; i++) {
                    String str_id = sList.get(i).getID();
                    if (str_id.equals("DM01-71_1") || str_id.equals("DM01-71_2") || str_id.equals("DM01-71_3")
                            || str_id.equals("DM01-71_4") || str_id.equals("")) {
                        ccsffl_list.add(sList.get(i));
                    }
                }
            }

            nice_spinner_clsffl.attachDataSource(ccsffl_list);
        } else if (flagName.equals("随诊方式")) {
            szfs_list = sList;
            nice_spinner_szfs.attachDataSource(szfs_list);
        } else if (flagName.equals("药物单位")) {
            ywdw_list = sList1;
            nice_spinner_dw1.attachDataSource(sList1);
            nice_spinner_dw2.attachDataSource(sList1);
            nice_spinner_dw3.attachDataSource(sList1);
        }
    }

    @Override
    public void setTizhong(String value) {
        ed_tz.setText(value);
    }

    @Override
    public void setXueya(String gaoya, String diya, String mailv) {

    }

    @Override
    public void setXueYang(String xueYang) {

    }
}
